header simpleGPS
{
	// This script shows how to use the UART to connect to a GPS module such as the
	// EM408 from Altronics, by Mauro Grassi. 
	// The USB Data Logger can automatically decode GPSS GPRMC NMEA sentences, but
	// other NMEA sentences can be decoded as well...
}

script simpleGPS
{
	// Create A new Log File for this script from scratch, named "GPSDecoder.txt"

	clearFile "GPSDecoder.txt";

	// Initialise the UART, pin D0 is Tx, pin D1 is Rx...
	// Baudrate 4800 bps is the default for the GPS module...

	// the special GPS decoding mode is set by using the define constant #GPSDecodingUART...
	
	@@openUART(#GPSDecodingUART + #noRxInvUART + #noTxInvUART, 4800, #D0, #D1);

	print "Sending commands to the GPS module...", newline;

	// Refer to the datasheet of the EMS408 for details...
	// The nmea built in command sends the output to the serial port, but it also keeps
	// a running XOR checksum that is appended to the end of the sentence for error checking...
	// The NMEA CRC is sent by using the print function pf(#nmea), as shown below. The following
	// command sets the output sentences for the EMS408 module to be GPSS GPRMC (recommended minimum
	// specific settings...
	
	nmea "$PSRF100,1,4800,8,1,0", pf(#nmea);
	nmea "$PSRF103,4,0,5,0", pf(#nmea);

        print "Waiting for GPS Sentences...", newline;
	precision(3);
	while(1)
	{
		if(@@receivedNMEAUART())
		{
			print newline, "Rx: [", pf(#serialInPipe), "]", newline;
			// #GPRMCNumBytesToMatch==32 is the number of bytes output 
			// by the internal automatic match for
			// GPRMC sentences...
			if($$nmea.outputPtr>=#GPRMCNumBytesToMatch)
			{
			print "Output: ", $$nmea.outputPtr, newline;
			print newline, "Longitude: ", @@abs($$longitude), " ";
			if($$longitude>=0)print "E"; else print "W";
			print newline, "Latitude : ", @@abs($$latitude), " ";
			if($$latitude>=0)print "N"; else print "S";
			print newline, "Speed    : ", $$speed,  " knots (over ground)";
			print newline, "Heading  : ", $$course, " degrees ";
			print newline, "GPS Time : ", pf(#vmTime), newline;
			}			
			@@clearUART();
			sleep(1);
			
		}
	}
}
